package com.ece.ecofitness.controller;

import java.util.Locale;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ece.ecofitness.model.User;
import com.ece.ecofitness.service.UserService;
import com.ece.ecofitness.service.ArticleService;
import com.ece.ecofitness.service.CommandeService;


/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {
	
	@Autowired
	private CommandeService commandeService;

	@Autowired
	private UserService userService;
	
	@Autowired
	private ArticleService articleService;
	
	@RequestMapping(value = "home.view", method = RequestMethod.GET)
	public String home(Model model,HttpServletRequest request, HttpServletResponse response) {
		String login=null;
		if(request.getCookies() !=null){
			for(Cookie c: request.getCookies()) {
		        if(c.getName().equals("login"))
		        {
		        	login=c.getValue();
		        	break;
		        }
		    }
			if(login!=null)
			{
				return "redirect:/profile.view";
			}
			else
				return "home";
		}
		else
			return "home";
	}
	
	@RequestMapping(value = "sign.view", method = RequestMethod.POST)
	public String sign(Locale locale, Model model,HttpServletRequest request, HttpServletResponse response) {
		
		String vue="";
		
		if(((String)request.getParameter("submit")).equals("Connexion"))
		{
			String login=(String)request.getParameter("pseudo");
			String password=(String)request.getParameter("password");
			User user=userService.getUser(login,password);
	
			if(user!=null)
			{
				response.addCookie(new Cookie("login", login));
				response.addCookie(new Cookie("passwd", password));
				
				return "redirect:/profile.view";
			}
			else{
				model.addAttribute("error", "Login ou mot de passe incorrecte");
				vue="home";
			}
		}
		
		else if((request.getParameter("submit")).equals("Deconnexion")){
			for(Cookie cookie : request.getCookies())
			{
					cookie.setValue("");
					cookie.setMaxAge(0);
					response.addCookie(cookie);
			}
			return "redirect:/home.view";
		}
		
		return vue;
	}
	
	
	@RequestMapping(value = "profile.view", method = RequestMethod.GET)
	public String profile( Model model,HttpServletRequest request, HttpServletResponse response) {
		String login=null;String vue="home";
		if(request.getCookies() !=null){
		    for(Cookie c: request.getCookies()) {
		        if(c.getName().equals("login"))
		        {
		        	login=c.getValue();
		        	break;
		        }
		    }
	    }
	    if(login!=null)
	    {
	    	  model.addAttribute("listArticles", articleService.getAllArticle());
	    	  model.addAttribute("listCommandes", commandeService.getAllCommande());
			  vue="profile";
	    }
	    else
	    	model.addAttribute("error", "Veuillez-vous connecter");
		
		return vue;
	}
	
	
}
